package com.admin.sys.base.module.quartz.jobs;

import com.admin.sys.base.module.extend.service.BaseService;
import com.admin.sys.base.module.message.service.SysMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 需要继承BaseService里的mybatis代理才能获取bean
 */
@Component
public class HelloJob extends BaseService {
    @Autowired
    private SysMessageService sysMessageService;

    public void getUser() {
        System.out.println("进入任务***********无参方法:batStart*************");
        System.out.println(Calendar.getInstance().get(Calendar.SECOND));
    }
    public void getUser(String params) {
        System.out.println("进入任务***********有参方法:getUser("+params+")*************");
        System.out.println(Calendar.getInstance().get(Calendar.SECOND));
    }

    /**
     * 推荐算法
     * 根据用户浏览量和收藏量进行推荐
     */
    //public final static int RECOMMEND_VIEW_NUM = 2;
    //public final static int RECOMMEND_COLLECT_NUM = 3;
    //@RequestMapping("/getRecommendList")
    //public ResultInfo getRecommendList() {
    //    //浏览量分布情况
    //    SysMessage viewMsg = new SysMessage();
    //    viewMsg.setType("浏览");
    //    List<SysMessage> viewList = sysMessageService.getList(viewMsg);
    //    List<Items> viewItemsList = new ArrayList<>();
    //    List<SysMessage> sortViewList = viewList.stream().sorted(Comparator.comparing(SysMessage::getStars).reversed()).collect(Collectors.toList());
    //    for (int i = 0; i < sortViewList.size(); i++) {
    //        SysMessage message = sortViewList.get(i);
    //        Items items = itemsService.get(message.getRefId());
    //        items.setRemarks("经常访问");
    //        viewItemsList.add(items);
    //        System.out.println("【名称】："+items.getName()+"【浏览量】："+message.getStars());
    //    }
    //    //收藏量分布情况
    //    SysMessage collectMsg = new SysMessage();
    //    collectMsg.setType("收藏");
    //    List<SysMessage> collectList = sysMessageService.getList(collectMsg);
    //    Map<String, IntSummaryStatistics> collectMap = collectList.stream().collect(Collectors.groupingBy(SysMessage::getRefId, Collectors.summarizingInt(SysMessage::getStars)));
    //    collectList.clear();
    //    for (Map.Entry<String, IntSummaryStatistics> entry : collectMap.entrySet()) {
    //        SysMessage sysMessage = new SysMessage();
    //        sysMessage.setRefId(entry.getKey());
    //        sysMessage.setStars((int) entry.getValue().getSum());
    //        collectList.add(sysMessage);
    //    }
    //    List<Items> collectItemsList = new ArrayList<>();
    //    List<SysMessage> sortCollectList = collectList.stream().sorted(Comparator.comparing(SysMessage::getStars).reversed()).collect(Collectors.toList());
    //    for (int i = 0; i < sortCollectList.size(); i++) {
    //        SysMessage message = sortCollectList.get(i);
    //        Items items = itemsService.get(message.getRefId());
    //        items.setRemarks("最受欢迎");
    //        collectItemsList.add(items);
    //        System.out.println("【名称】："+items.getName()+"【收藏量】："+message.getStars());
    //    }
    //    //设置推荐数量:去重
    //    List<Items> recommendAll = new ArrayList<>();
    //    recommendAll.addAll(viewItemsList.size() > RECOMMEND_VIEW_NUM ? viewItemsList.subList(0, RECOMMEND_VIEW_NUM) : viewItemsList);
    //    recommendAll.addAll(collectItemsList.size() > RECOMMEND_COLLECT_NUM ? collectItemsList.subList(0, RECOMMEND_COLLECT_NUM) : collectItemsList);
    //    ArrayList<Items> recommendList = recommendAll.stream().collect(Collectors.collectingAndThen(
    //            Collectors.toCollection(() -> new TreeSet<>(
    //                    Comparator.comparing(
    //                            Items::getId))), ArrayList::new));
    //    return ResultInfo.ok("获取用户推荐数据", recommendList);
    //}

}